package com.smartrobot.smartrobot.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Environment;
import android.util.Log;

public class PictureFromCammera {

    private final static int MEDIA_TYPE_IMAGE = 1;

    /** 以独特取名方式给创建图片文件 */
    public static File getOutputMediaFile(int type) {

        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "CameraPicture");

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("CameraPicture", "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                .format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else {
            return null;
        }

        return mediaFile;
    }


    // 使用图片时 会出现oom现象 因此需要判断图片并且若是大于就缩小
    public static Bitmap scaleBitmap(String path) {

        int IMG_MAX_WIDTH_SIZE = 1024;
        int IMG_MAX_HEIGHT_SIZE = 768;

        if (path == null)
            throw new NullPointerException();

        BitmapFactory.Options option = new BitmapFactory.Options();
        option.inJustDecodeBounds = true; // 只是读取文件信息，不进行分配内存
        BitmapFactory.decodeFile(path, option);

        // 计算出图片应该按比例缩小大小
        int scale = 1;
        while (option.outWidth / scale > IMG_MAX_WIDTH_SIZE
                || option.outHeight / scale > IMG_MAX_HEIGHT_SIZE)
            scale *= 2;

        option.inJustDecodeBounds = false;// 读取图片到内存
        option.inSampleSize = scale;// 设置应该缩小的大小

        return rotaingImageView(readPictureDegree(path),BitmapFactory.decodeFile(path, option));
    }

    /**
     * 读取图片属性：旋转的角度
     * @param path 图片绝对路径
     * @return degree旋转的角度
     */
    public static int readPictureDegree(String path) {
        int degree  = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /*
     * 旋转图片
     * @param angle
     * @param bitmap
     * @return Bitmap
     */
    public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();;
        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);
        // 创建新的图片

        return  Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);

    }
	
}
